package com.study.controller;

import com.study.model.Resources;
import com.study.model.User;
import com.study.service.ResourcesService;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @ClassName: HomeController
 * @Description: TODO
 * @author: lyg
 * @date: 2017年9月20日 上午10:29:36
 */
@Controller
public class HomeController {
	@Resource
	private ResourcesService resourcesService;

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login() {
		return "login";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(HttpServletRequest request, User user, Model model) {
		if (StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())) {
			request.setAttribute("msg", "用户名或密码不能为空！");
			return "login";
		}
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
		try {
			subject.login(token);
			return "redirect:index";
		} catch (LockedAccountException lae) {
			token.clear();
			request.setAttribute("msg", "用户已经被锁定不能登录，请与管理员联系！");
			return "login";
		} catch (AuthenticationException e) {
			token.clear();
			request.setAttribute("msg", "用户或密码不正确！");
			return "login";
		}
	}

	/**
	 * 首页
	 * 
	 * @Title: index
	 * @Description: TODO
	 * @return
	 * @return: String
	 */
	@RequestMapping(value = { "/index", "" })
	public ModelAndView index(ModelAndView modelAndView) {
		Map<String, Object> map = new HashMap<>();
		Integer userid = (Integer) SecurityUtils.getSubject().getSession().getAttribute("userSessionId");
		map.put("type", 1);
		map.put("userid", userid);
		List<Resources> resourcesList = resourcesService.loadUserResources(map);
		modelAndView.addObject("list",resourcesList);
		modelAndView.setViewName("index");
		modelAndView.addObject("html", "home");
		return modelAndView;
	}

	@RequestMapping("/rolesPage")
	public String rolesPage() {
		return "role/roles";
	}

	@RequestMapping("/resourcesPage")
	public String resourcesPage() {
		return "resources/resources";
	}

	@RequestMapping("/textTypesetting")
	public ModelAndView textTypesetting(ModelAndView modelAndView) {
		modelAndView.setViewName("index");
		modelAndView.addObject("html", "typography");
		return modelAndView;
	}

	@RequestMapping("/home")
	public ModelAndView home(ModelAndView modelAndView) {
		modelAndView.setViewName("index");
		modelAndView.addObject("html", "home");
		return modelAndView;
	}
	@RequestMapping("/elements")
	public ModelAndView elements(ModelAndView modelAndView) {
		modelAndView.setViewName("index");
		modelAndView.addObject("html", "elements");
		Map<String, Object> map = new HashMap<>();
		Integer userid = (Integer) SecurityUtils.getSubject().getSession().getAttribute("userSessionId");
		map.put("type", 1);
		map.put("userid", userid);
		List<Resources> resourcesList = resourcesService.loadUserResources(map);
		modelAndView.addObject("list",resourcesList);
		modelAndView.addObject("openUrl","elements");
		return modelAndView;
	}
	@RequestMapping("/403")
	public ModelAndView forbidden(ModelAndView modelAndView) {
		modelAndView.setViewName("403");
		modelAndView.addObject("html", "typography");
		return modelAndView;
	}
}
